package com.op.teachingliteapp.fragment.resourcedetails;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.android_file_manger.MainActivity;
import com.op.teachingliteapp.R;
import com.op.teachingliteapp.fragment.viewer.ViewPagerFragment;
import com.op.teachingliteapp.util.ScreenSendUtils;
import org.vudroid.pdfdroid.codec.PdfContext;

/*
 * //这个fragment写选择列表
 */
public class ResourceDetailsFragment extends Fragment {
	private static final String sdcardPath = Environment
			.getExternalStorageDirectory().toString();
	public static String Location = sdcardPath + "/DEMO/";
	private View root;

	@SuppressWarnings("unused")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		MainActivity.fileName = null;//清空文件管理器选中的文件
		Log.i("PPT列表", sdcardPath);
		View root = inflater.inflate(R.layout.fragment_resource_details_ppt,
				container, false);
		String url = Environment.getExternalStorageDirectory().getPath();
		String url2 = Environment.getExternalStorageDirectory().getPath();
		ListView mListView = (ListView) root.findViewById(R.id.ppt_listView);
		ArrayList<ResourceDetailsDataProvider> ppt_list = new ArrayList<ResourceDetailsDataProvider>();
//		if (ppt_list == null) {
			ppt_list.add(new ResourceDetailsDataProvider("第1章 Java概述", Location
					+ "java_1.pdf", R.drawable.pdf));
			ppt_list.add(new ResourceDetailsDataProvider("第2章 Java语言基础",
					Location + "java_2.pdf", R.drawable.pdf));
			ppt_list.add(new ResourceDetailsDataProvider("第3章 类的封装、继承和多态",
					Location + "java_3.pdf", R.drawable.pdf));
			ppt_list.add(new ResourceDetailsDataProvider("第4章 接口、内部类和包",
					Location + "java_4.pdf", R.drawable.pdf));
			ppt_list.add(new ResourceDetailsDataProvider("第5章 异常处理", Location
					+ "java_5.pdf", R.drawable.pdf));
			ppt_list.add(new ResourceDetailsDataProvider("第6章 图形用户界面", Location
					+ "java_6.pdf", R.drawable.pdf));
			ppt_list.add(new ResourceDetailsDataProvider("第7章 多线程", Location
					+ "java_7.pdf", R.drawable.pdf));
			ppt_list.add(new ResourceDetailsDataProvider("云教学", Location
					+ "yunjiaoxue.pdf", R.drawable.pdf));
            ppt_list.add(new ResourceDetailsDataProvider("Spring Framework 3教程", Location
                    + "SpringFramework3.pdf", R.drawable.pdf));
        ppt_list.add(new ResourceDetailsDataProvider("系统开发和运行知识", Location
                + "系统开发和运行知识.pdf", R.drawable.pdf));
//        系统开发和运行基础知识习题.pdf
        ppt_list.add(new ResourceDetailsDataProvider("系统开发和运行基础知识习题", Location
                + "系统开发和运行基础知识习题.pdf", R.drawable.pdf));
//		} else {
//			ppt_list.clear();
//		}

		final ResourceDetailsAdapter mAdapter = new ResourceDetailsAdapter(
				getActivity(), ppt_list);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				ResourceDetailsDataProvider resourceDetailsDataProvider = mAdapter
						.getItem(position);

				String url = resourceDetailsDataProvider.getUrl();
                if (isPdfOpenable(url)) {
                    ViewPagerFragment pptShow = new ViewPagerFragment();// 这个是打开pdf速度快，没有画笔的
                    // PPTShowFragment pptShow= new
                    // PPTShowFragment();//这个是打开pdf速度慢，有画笔的
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    pptShow.setArguments(bundle);

                    getFragmentManager().beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.main_show2, pptShow).commit();
                } else {
                    Toast.makeText(getActivity(), "该文件无法打开", Toast.LENGTH_SHORT).show();
                }


			}

		});

		Button add_ppt_btn = (Button) root.findViewById(R.id.add_ppt_btn);
		add_ppt_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), MainActivity.class);
				startActivity(intent);
			}
		});
		this.root = root;
		return root;
	}

	@Override
	public void onResume() {
		// 用于在文件管理器里打开pdf后fragment显示
		if (MainActivity.fileName != null
				&& MainActivity.fileName.endsWith(".pdf")) {
			ViewPagerFragment pptShow = new ViewPagerFragment();

			Bundle bundle = new Bundle();
			bundle.putString("url", MainActivity.filePath);
			pptShow.setArguments(bundle);

			getFragmentManager().beginTransaction().addToBackStack(null)
					.replace(R.id.main_show2, pptShow).commit();
			MainActivity.fileName = null;
		} else {
			ScreenSendUtils.sendWithDely(root);
		}

		super.onResume();
	}
    private boolean isPdfOpenable(String url) {

        PdfContext pdfContext = new PdfContext();
        try {
            pdfContext.openDocument(url);
        } catch (Exception e) {
            return false;
        }


        return true;
    }
}
